# arm linux windows
BUILD_TYPE ?= 

# 颜色
RED := \033[0;31m
GREEN := \033[0;32m
BLUE := \033[0;34m
NC := \033[0m

CC := gcc
CROSS_COMPILE := 

# LVGL_PATH := $(shell pwd)/lvgl
LVGL_PATH := ./lvgl
include $(LVGL_PATH)/lvgl.mk

# 定义库文件名
LIB_NAME := liblvgl.a

CFLAGS := -I. -I$(LVGL_TOP_PATH) -I$(LVGL_PATH) -I$(LVGL_PATH)/src 
CFLAGS += -I../freetype/include/freetype2 \

# 定义链接选项
LDFLAGS := -lm \
            -lz \
            -lbz2 \
            -lfreetype \
            -ldrm \
            -lwayland-client \
            -lwayland-cursor \
			-lrt
####### build arm #######
ifeq ($(BUILD_TYPE), arm)
CFLAGS += -I../freetype/include/freetype2 \
        -I/opt/atk-dlrk3568-5_10_sdk-toolchain/aarch64-buildroot-linux-gnu/sysroot/usr/include/libdrm \
        -I/opt/atk-dlrk3568-5_10_sdk-toolchain/aarch64-buildroot-linux-gnu/sysroot/usr/include/drm \
        -I/opt/atk-dlrk3568-5_10_sdk-toolchain/aarch64-buildroot-linux-gnu/sysroot/usr/include/ 

LDFLAGS += -L../freetype/arm \
            -L/opt/atk-dlrk3568-5_10_sdk-toolchain/aarch64-buildroot-linux-gnu/sysroot/usr/lib 
else ifeq ($(BUILD_TYPE), linux)
####### build linux #######
LDFLAGS += -L../freetype/linux
endif

# 定义目标文件
AOBJS := $(ASRCS:.c=.o)
COBJS := $(CSRCS:.c=.o)
CXXOBJS := $(CXXSRCS:.c=.o)
# 总文件数量
TOTAL_FILES := $(words $(ASRCS) $(CSRCS) $(CXXSRCS))
# 已编译文件数量
COMPILED_FILES = 0

# 编译LVGL源文件并创建静态库
$(LIB_NAME): $(AOBJS) $(COBJS) $(CXXOBJS)  
	@printf "$(NC)$(RED)CROSS_COMPILE: $(CROSS_COMPILE) build type: $(BUILD_TYPE)$(NC)\n"
	@echo "Archiving: lib/lvgl/$@"
	@$(CROSS_COMPILE)$(AR) rcs $@ $^

%.o: %.c
	$(eval COMPILED_FILES=$(shell expr $(COMPILED_FILES) + 1))
	$(eval PERCENT=$(shell expr $(COMPILED_FILES) \* 100 / $(TOTAL_FILES)))
	@printf "$(NC)$(GREEN)Building [%%$(PERCENT)]: $@$(NC)\n"
	@$(CROSS_COMPILE)$(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS)

# 清理编译生成的文件
clean:
	@echo "Clean lvgl"
	@rm -f ./lib/lvgl/liblvgl.a
	@rm -f $(AOBJS) $(COBJS) $(CXXOBJS) $(LIB_NAME)

.PHONY: all clean
